package org.grayrabbit.cms.publish.generate;

import java.io.File;
import java.util.Date;

import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.PublishLog;
import org.grayrabbit.cms.entity.PublishTask;
import org.grayrabbit.cms.entity.Survey;
import org.grayrabbit.cms.publish.api.Generatable;
import org.grayrabbit.cms.util.FilePathSptUtil;

import freemarker.template.Configuration;

/**
 * @author Administrator
 * @version 1.0
 * @created 2011-6-15 10:43:13
 */
public class SurveyGenerater extends AbstractGenerater {
	
	private static String htmlFileNameExt = ".html";
	private static String surveyPath = "survey";
	
	private Survey survey;

	public SurveyGenerater(Configuration configuration, String path, Survey survey,Generatable generatable,Admin admin) {
		super(configuration, path,generatable,admin);
		this.survey = survey;
	}

	@Override
	protected File getTargetFile() {
		return new File(FilePathSptUtil.LOCAL_SITE_PUBLISH_PATH+FilePathSptUtil.padRightSpt(survey.getSite().getSiteShortName())+FilePathSptUtil.padRightSpt(surveyPath)+survey.getSurveyId()+htmlFileNameExt);
	}

	@Override
	protected void initRootMap() {
		rootMap.put("survey", survey);
		rootMap.put("site",survey.getSite());
		
	}

	@Override
	protected void logInfo(PublishTask publishTask,boolean success,Throwable t) {
		PublishLog publishLog = new PublishLog();
		publishLog.setHtmlPath(getTargetFile().toString());
		publishLog.setIsError(Boolean.valueOf(!success));
		publishLog.setObjectId(survey.getSurveyId());
		if(success){
			publishLog.setPublishInfo("生成成功");
		}else{
			if(t!=null){
				publishLog.setPublishInfo("生成失败:"+t.getMessage());
			}else{
				publishLog.setPublishInfo("生成失败");
			}
		}
		publishLog.setPublishStatus(PublishTask.STATUS_FINISHED);
		publishLog.setPublishTime(new Date());
		publishLog.setPublishType("survey");
		publishLog.setTemplatePath(this.path);
		publishLog.setPublishTask(publishTask);
		generatable.savePublishLog(publishLog);
		calcTaskInfo(publishTask,success);
	}
	public String toString(){
		return "survey:"+survey.getSurveyId();
	}
}